import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/lock/api.dart";
import "package:ct_pa_majordomo/pages/device/lock/password_key/widgets/ope_record/index.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/router/keys.dart";
import "package:ct_pa_majordomo/utils/date_tools.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:ct_pa_majordomo/widgets/tai_dialog/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class Detail extends StatelessWidget {
  final Map info;
  const Detail({super.key, required this.info});

  @override
  Widget build(BuildContext context) {
    Future<void> delPassword() async {
      print("${info["lock"]["lockId"]}, ${info["keyboardPwdId"]}");
      print(info);
      /// 删除密码
      taiDialog(
        context: context,
        title: "确定删除密码？",
        child: const Text("删除密码后，密码将无法使用。",textAlign: TextAlign.center,),
        onConfirm: (BuildContext taiContext) async {
          try {
            EasyLoading.show();
            await lockApi.deletePassword(int.parse(info["lock"]["lockId"]), info["keyboardPwdId"]);
            EasyLoading.showToast("删除密码成功");
            Stacker.pushNamedAndRemoveUntil(Keys.DEVICE_LOCK_PASSWORDKEY, ModalRoute.withName(Keys.DEVICE_LOCK_DETAIL), arguments: info);
          } catch (error) {
            print("error: $error");
          }
        },
      );
    }
    return Scaffold(
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.white),
        title: const Text("密码详情", style: TextStyle(color: Colors.white),),
        centerTitle: true,
        backgroundColor: ThemeVars.colorPrimaryDark,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: ListView(
        children: [
          MenuBlock(
            borderRadius: const BorderRadius.all(Radius.circular(0)),
            items: [
              MenuBlockItem(
                text: "密码",
                showArrow: false,
                suffix: Text(info["keyboardPwd"], style: TextStyle(color: ThemeVars.colorTextTer),),
              ),
              MenuBlockItem(
                text: "姓名",
                showArrow: false,
                suffix: Text(info["keyboardPwdName"], style: TextStyle(color: ThemeVars.colorTextTer),),
              ),
              MenuBlockItem(
                text: "有效期",
                  showArrow: false,
                suffix: info["endDate"] == 0
                    ? const Text("永久", style: TextStyle(color: ThemeVars.colorTextTer),)
                    : Column(
                        children: [
                          Text(DateTools.formatYMD(info["startDate"], format: "{y}-{m}-{d} {h}:{i}"), style: TextStyle(color: ThemeVars.colorTextTer),),
                          Text(DateTools.formatYMD(info["endDate"], format: "{y}-{m}-{d} {h}:{i}"), style: TextStyle(color: ThemeVars.colorTextTer),),
                        ],
                )
              )
            ],
          ),
          const SizedBox(height: 12,),
          MenuBlock(
            borderRadius: const BorderRadius.all(Radius.circular(0)),
            items: [
              MenuBlockItem(
                text: "发送人",
                showArrow: false,
                suffix: Text(info["senderUsername"], style: TextStyle(color: ThemeVars.colorTextTer),),
              ),
              MenuBlockItem(
                text: "发送时间",
                showArrow: false,
                suffix: Text(DateTools.formatYMD(info["sendDate"], format: "{y}-{m}-{d} {h}:{i}"), style: TextStyle(color: ThemeVars.colorTextTer),),
              ),
            ],
          ),
          const SizedBox(height: 12,),
          MenuBlock(
            borderRadius: const BorderRadius.all(Radius.circular(0)),
            items: [
              MenuBlockItem(
                text: "操作记录",
                onTap: () {
                  Stacker.pushNamed(Keys.DEVICE_LOCK_PASSWORDKEY_OPE_RECORD, arguments: info);
                },
              ),
            ],
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(ThemeVars.paddingPage, 40, ThemeVars.paddingPage, 0),
            child: Button(
              radius: 5,
              type: ButtonType.danger,
              onPressed: () {
                delPassword();
              },
              child: const Text("删除"),
            ),
          ),
        ],
      ),
    );
  }

}